/*
 * CS_EXCHANGE_SCORE_REQ.cpp
 *
 *  Created on: 2013. 9. 27.
 *      Author: mega@gamevil.com
 */

#include "../../User.h"
#include "../../Command.h"
#include "../../NLogic/Buff.h"
#include "../../NLogic/Score.h"
#include "../../NUtil/Utils.h"
#include "../../NResource/Static.h"

namespace ProjectZ { namespace NCommand {

void SendFailExchangeScore(User * pUser, int ack)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ack);
	pAck->MakePacket(CMD_SC_EXCHANGE_SCORE_REQ);
	pUser->Send(pAck);
}

void Command::CS_EXCHANGE_SCORE_REQ(User * pUser, ::Network::Packet * pPacket)
{
/*	/// 승점 교환 요청
	CS_EXCHANGE_SCORE_REQ				0x1302
	{
	}

	SC_EXCHANGE_SCORE_REQ				0x1303
	{
		s16				s16ack;
		s16				s16buff_skill_tid;		//버프 tid
		u8				u8skill_level;
		s16				s16skill_value;
		u16				u16buff_time;		//버프 시간 (분단위)
	}*/

	PREPARE_PACKET;

	if (10 > pUser->GetCharacterInfo().level)
	{
		SendFailExchangeScore(pUser, ACK_UNKNOWN_ERROR);
		DEBUGLOG("%s : INVALID Character level", __FUNCTION__);
		return;
	}

	if (pUser->GetUserInfo().wp_last_week < static_cast<uint32_t>(PVP_BUFF_MAX_SCORE))
	{
		SendFailExchangeScore(pUser, ACK_UNKNOWN_ERROR);
		DEBUGLOG("%s : WinPoint Lack!!", __FUNCTION__);
		return;
	}

	NLogic::Buff * pBuff = pUser->GetBuff();
	if (NULL == pBuff)
	{
		SendFailExchangeScore(pUser, ACK_UNKNOWN_ERROR);
		DEBUGLOG("%s : pBuff is NULL!!", __FUNCTION__);
		return;
	}

	NLogic::Buff::Data * pBuffData = pBuff->GetData(NLogic::Buff::BuffType::WINPOINT);
	if (NULL == pBuffData)
	{
		SendFailExchangeScore(pUser, ACK_UNKNOWN_ERROR);
		DEBUGLOG("%s : pBuffData is NULL!!", __FUNCTION__);
		return;
	}

	NLogic::Score * pScore = pUser->GetScore();
	if (NULL == pScore)
	{
		SendFailExchangeScore(pUser, ACK_UNKNOWN_ERROR);
		DEBUGLOG("%s : pScore is NULL!!", __FUNCTION__);
		return;
	}

	// 버프 지속 시간 계산
	int continueMinute = NResource::Static::InstancePtr()->GetBuffTimeResource()->GetWinPointBuffContinueMinute(pUser);
	if (0 >= continueMinute)
	{
		SendFailExchangeScore(pUser, ACK_UNKNOWN_ERROR);
		DEBUGLOG("%s : INVALID Buff Time. minute=%d, company=%d, salecode=%d", __FUNCTION__, continueMinute, pUser->GetCompany(), pUser->GetSaleCode());
		return;
	}


	// 버프 정보 설정
	pBuffData->u8type			= static_cast<uint8_t>(NLogic::Buff::BuffType::WINPOINT);
	pBuffData->u16skill_tid		= NUtil::GetRandBetween(PVP_BUFF_START_TID, PVP_BUFF_END_TID);
	pBuffData->u8skill_level	= NUtil::GetRandBetween(1, 5);
	pBuffData->s16skill_value	= 0;
	pBuffData->_regdate			= time(NULL) + (continueMinute * 60);

	// 승점 차감
	pUser->GetUserInfo().wp_last_week -= PVP_BUFF_MAX_SCORE;
	pScore->SetMemcache();

	// 응답
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ACK_OK);
	*pAck << static_cast<int16_t>(pBuffData->u16skill_tid);
	*pAck << static_cast<uint8_t>(pBuffData->u8skill_level);
	*pAck << static_cast<int16_t>(pBuffData->s16skill_value);
	*pAck << static_cast<uint16_t>(continueMinute);
	*pAck << pUser->GetUserInfo().wp_last_week;
	pAck->MakePacket(CMD_SC_EXCHANGE_SCORE_REQ);
	pUser->Send(pAck);
}

} /*NCommand*/ } /*ProjectZ*/
